package com.grupit.module.user.web.gwt.client.pluggablefeatures.ui;

import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.HasAllMouseHandlers;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.event.dom.client.MouseWheelEvent;
import com.google.gwt.event.dom.client.MouseWheelHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.cellview.client.CellWidget;
import com.google.gwt.user.client.ui.HasHTML;
import com.seleniti.pozit.controller.model.integration.database.domain.PoZitNote;
import com.seleniti.pozit.web.desktop.client.ui.cell.PoZitCell;
import com.seleniti.pozit.web.desktop.client.ui.cell.PoZitCell.Color;

public class PoZitWidget extends CellWidget<PoZitNote> implements HasAllMouseHandlers, HasHTML {

	public PoZitWidget() {
		super(new PoZitCell(Color.YELLOW, new FieldUpdater<PoZitNote, String>() {

			@Override
			public void update(int index, PoZitNote object, String value) {
				object.setNoteText(value);
				// presenter.onPoZitChanged(object);
			}
		}) {

			@Override
			public void onMouseDown(com.google.gwt.cell.client.Cell.Context context, Element parent, PoZitNote value,
					NativeEvent event, ValueUpdater<PoZitNote> valueUpdater) {
				// Nothing to do
			}

			@Override
			public void onMouseUp(com.google.gwt.cell.client.Cell.Context context, Element parent, PoZitNote value,
					NativeEvent event, ValueUpdater<PoZitNote> valueUpdater) {
				// Nothing to do

			}

			@Override
			public void onMouseMove(com.google.gwt.cell.client.Cell.Context context, Element parent, PoZitNote value,
					NativeEvent event, ValueUpdater<PoZitNote> valueUpdater) {
				// Nothing to do

			}

		});
	}

	@Override
	public HandlerRegistration addMouseDownHandler(MouseDownHandler handler) {
		return addDomHandler(handler, MouseDownEvent.getType());
	}

	@Override
	public HandlerRegistration addMouseUpHandler(MouseUpHandler handler) {
		return addDomHandler(handler, MouseUpEvent.getType());
	}

	@Override
	public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
		return addDomHandler(handler, MouseOutEvent.getType());
	}

	@Override
	public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
		return addDomHandler(handler, MouseOverEvent.getType());
	}

	@Override
	public HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) {
		return addDomHandler(handler, MouseMoveEvent.getType());
	}

	@Override
	public HandlerRegistration addMouseWheelHandler(MouseWheelHandler handler) {
		return addDomHandler(handler, MouseWheelEvent.getType());
	}

	@Override
	public String getText() {
		return this.getValue() != null ? this.getValue().getNoteText() : "";
	}

	@Override
	public void setText(String text) {
		if (this.getValue() == null) {
			this.setValue(new PoZitNote());
		}

		this.getValue().setNoteText(text);
	}

	@Override
	public String getHTML() {
		return this.getValue() != null ? this.getValue().getNoteText() : "";
	}

	@Override
	public void setHTML(String html) {
		if (this.getValue() == null) {
			this.setValue(new PoZitNote());
		}

		this.getValue().setNoteText(html);
	}

}
